package com.yunhai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunhai.common.core.domain.AjaxResult;
import com.yunhai.common.exception.BaseException;
import com.yunhai.system.domain.Product;
import com.yunhai.system.domain.ProductType;
import com.yunhai.system.domain.admin.ProductAdminVo;
import com.yunhai.system.domain.vo.ProductVo;
import com.yunhai.system.mapper.ProductMapper;
import com.yunhai.system.service.ProductService;
import com.yunhai.system.service.ProductTypeService;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 产品表(Product)表服务实现类
 *
 * @author makejava
 * @since 2021-01-09 17:53:32
 */
@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Resource
    private ProductTypeService productTypeService;

    /**
     * 获取产品详情
     *
     * @param param
     * @return
     */
    @Override
    public AjaxResult info(Map<String, Object> param) {
        Integer id = MapUtils.getInteger(param, "id");
        Product product = getById(id);
        if (null == product) {
            throw new BaseException("产品不存在");
        }
        //获取详情
        QueryWrapper<Product> childWrapper = new QueryWrapper<>();
        childWrapper.eq("parent_id", product.getId());
        childWrapper.ne("id", product.getId());

        List<Product> childList = list(childWrapper);
        ProductVo productVo = new ProductVo();
        try {
            PropertyUtils.copyProperties(productVo, product);
            productVo.setId(product.getId());
            productVo.setContent(product.getContent());
            productVo.setImg(product.getImg());
            productVo.setTypeId(product.getTypeId());
            productVo.setParentId(product.getParentId());
            productVo.setProductName(product.getProductName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (CollectionUtils.isNotEmpty(childList)) {
            productVo.setChildList(childList);
        }
        return AjaxResult.success(productVo);
    }


    /**
     * 后台列表查询
     *
     * @param product
     * @return
     */
    @Override
    public List<ProductAdminVo> queryList(Product product) {
        LambdaQueryWrapper<Product> lqw = Wrappers.lambdaQuery();

        if (StringUtils.isNotBlank(product.getProductName())) {
            lqw.like(Product::getProductName, product.getProductName());
        }
        if (product.getTypeId() != null) {
            lqw.eq(Product::getTypeId, product.getTypeId());
        }
        if (product .getLanguage() != null) {
            lqw.eq(Product::getLanguage, product.getLanguage());
        }
        if (product.getCreatedDate() != null) {
            lqw.eq(Product::getCreatedDate, product.getCreatedDate());
        }
        if (product.getParentId() != null) {
            lqw.eq(Product::getParentId, product.getParentId());
        }
        lqw.orderByDesc(Product::getCreatedDate);
        List<Product> productList = this.list(lqw);
        List<ProductAdminVo> productVos = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(productList)) {
            List<Long> typeIds = productList.stream().map(Product::getTypeId).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(typeIds)) {
                LambdaQueryWrapper<ProductType> lqwType = Wrappers.lambdaQuery();
                lqwType.in(ProductType::getId, typeIds);
                List<ProductType> productTypeList = productTypeService.list(lqwType);
                if (CollectionUtils.isNotEmpty(productTypeList)) {
                    for (Product p : productList) {
                        ProductAdminVo productVo = new ProductAdminVo();
                        try {
                            productVo.setProductName(p.getProductName());
                            productVo.setContent(p.getContent());
                            productVo.setImg(p.getImg());
                            productVo.setParentId(p.getParentId());
                            productVo.setLanguage(p.getLanguage());
                            productVo.setId(p.getId());
                            productVo.setCreatedDate(p.getCreatedDate());
                            productVo.setTypeId(p.getTypeId());
                            productVo.setListPic(p.getListPic());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        for (ProductType type : productTypeList) {
                            if (null != (type.getId())) {
                                if (p.getTypeId().equals(type.getId())) {
                                    productVo.setTypeName(type.getTypeName());
                                    break;
                                }
                            }

                        }
                        productVos.add(productVo);
                    }
                }

            }
        }
        return productVos;
    }

    /**
     * api获取列表
     * @param param
     * @return
     */
    @Override
    public List<Product> productList(Map<String, Object> param) {

        Integer type = MapUtils.getInteger(param, "type");
        String keyword = MapUtils.getString(param, "keyword");
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("language", param.get("language"));
        if (null != type) {
            productQueryWrapper.eq("type_id", type);
            productQueryWrapper.eq("parent_id", 0);
        }
        if (StringUtils.isNotBlank(keyword)) {
            productQueryWrapper.like("product_name", keyword);
        }
        List<Product> list = list(productQueryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(data ->{
                data.setImg(data.getListPic());
            });
        }
        return list;

    }
}